﻿using System; 
using System.Net.NetworkInformation;
using System.Collections;
using System.Net;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;

namespace NetAdapterTrace
{
    public class NwAdapterInfo
    {
        public int SlNo
        {
            get;
            set;
        }

        public string Name { get; set; }
        public string Description { get; set; }
        public string Type { get; set; }
        public string IpAddress { get; set; }
        public string GateWay { get; set; }
        public string DNSServers { get; set; }
        public string Status { get; set; }
        public long MaxSpeed { get; set; }
        public string Versions { get; set; }
        static int Cntr;
        public static NwAdapterInfo DisplayAdapterInfo(NetworkInterface adapter, BindingSource bsList,NwAdapterInfo nwItem )
        {
                
            nwItem.Name = adapter.Name;
            nwItem.Description = adapter.Description;
           
            bsList.Add(adapter.Description);
            bsList.Add(String.Empty.PadLeft(adapter.Description.Length, '='));
            bsList.Add(string.Format("  Interface type .......................... : {0}", adapter.NetworkInterfaceType));
            bsList.Add(string.Format("  Physical Address ........................ : {0}",
                       adapter.GetPhysicalAddress().ToString()));
            bsList.Add(string.Format("  Operational status ...................... : {0}",
                adapter.OperationalStatus));
            nwItem.Status = adapter.OperationalStatus.ToString() ;
            nwItem.MaxSpeed = adapter.Speed ;
            nwItem.Type = adapter.NetworkInterfaceType.ToString();    
            nwItem.SlNo = ++Cntr ;
 
            string versions = "";

            // Create a display string for the supported IP versions.
            if (adapter.Supports(NetworkInterfaceComponent.IPv4))
            {
                versions = "IPv4";
            }
            if (adapter.Supports(NetworkInterfaceComponent.IPv6))
            {
                if (versions.Length > 0)
                {
                    versions += " ";
                }
                versions += "IPv6";
            }
            nwItem.Versions = versions; 
            bsList.Add(string.Format("  IP version .............................. : {0}", versions));
            IPInterfaceProperties properties = adapter.GetIPProperties();
            ShowIPAddresses(properties, bsList,nwItem );

            // The following information is not useful for loopback adapters.
            if (adapter.NetworkInterfaceType == NetworkInterfaceType.Loopback)
            {
                return nwItem ;
            }
            bsList.Add(string.Format("  DNS suffix .............................. : {0}",
                properties.DnsSuffix));


            if (adapter.Supports(NetworkInterfaceComponent.IPv4))
            {
                IPv4InterfaceProperties ipv4 = properties.GetIPv4Properties();
                if (ipv4 != null)
                {
                    bsList.Add(string.Format("  MTU...................................... : {0}", ipv4.Mtu));
                    if (ipv4.UsesWins)
                    {

                        IPAddressCollection winsServers = properties.WinsServersAddresses;
                        if (winsServers.Count > 0)
                        {
                            bsList.Add("  WINS Servers ............................ :");
                            GetIpAsString(winsServers,  bsList);
                        }
                    }
                }
            }

            bsList.Add(string.Format("  DNS enabled ............................. : {0}",
                properties.IsDnsEnabled));
            bsList.Add(string.Format("  Dynamically configured DNS .............. : {0}",
                properties.IsDynamicDnsEnabled));
            bsList.Add(string.Format("  Receive Only ............................ : {0}",
                adapter.IsReceiveOnly));
            bsList.Add(string.Format("  Multicast ............................... : {0}",
                adapter.SupportsMulticast));
            return nwItem;
        }

        static StringBuilder sbProp = new StringBuilder(); 
        public static string  GetIpAsString(IPAddress[] iPAddressList, BindingSource bsList)
        {
            sbProp.Length=0;   
            if (iPAddressList == null)
                return null ;
            bsList.Add("IPAddress is : ");
            foreach (IPAddress ipItem in iPAddressList)
            {
                sbProp.AppendFormat(" {0}, ",  ipItem.ToString());
            }
            return sbProp.ToString();    
        }

        public static   void ShowIPAddresses(IPInterfaceProperties properties, BindingSource bsList,NwAdapterInfo nwItem )
        {
            if (properties == null)
                return;
            
            bsList.Add("Dns Addresses : -");
            nwItem.DNSServers =  GetIpAsString(properties.DnsAddresses,bsList );
            bsList.Add(nwItem.DNSServers);   
            bsList.Add("GateWay Addresses: ");
            nwItem.GateWay =  GetIpAsString(properties.GatewayAddresses,bsList );
            bsList.Add(nwItem.GateWay);  
            bsList.Add("AnyCast Addresses: ");
            bsList.Add(GetIpAsString(properties.AnycastAddresses, bsList));

        }

        private static string GetIpAsString(IPAddressInformationCollection iPAddressInformationCollection, BindingSource bsList)
        {
            if (iPAddressInformationCollection == null)
                return null;
            sbProp.Length=0;
            foreach (IPAddressInformation ipAddr in iPAddressInformationCollection)
            {
                bsList.Add(ipAddr.Address.ToString());
                sbProp.AppendFormat(" {0}, ", ipAddr.Address.ToString());
            }
           return sbProp.ToString();   
           
        }

        private static  string GetIpAsString(GatewayIPAddressInformationCollection gatewayIPAddressInformationCollection, BindingSource bsList)
        {
            if (gatewayIPAddressInformationCollection == null)
                return null;
            sbProp.Length =0;
            foreach (GatewayIPAddressInformation ipAddr in gatewayIPAddressInformationCollection)
            {
                bsList.Add(ipAddr.Address.ToString());
                sbProp.AppendFormat(" {0}, ", ipAddr.Address.ToString());
            }
            return sbProp.ToString();  
            
        }

        public static string GetIpAsString(IPAddressCollection iPAddressCollection, BindingSource bsList)
        {
            if (iPAddressCollection == null)
                return null;
            sbProp.Length=0;
            foreach (System.Net.IPAddress ipAddr in iPAddressCollection)
            {
                
                sbProp.AppendFormat(" {0}, ", ipAddr.ToString());
            }
            return (sbProp.ToString());    
        }

    }
}
